home *** CD-ROM | disk | FTP | other *** search
/ 130 MIDI Tool Box / 130 MIDI Tool Box.iso / ravelutl / cfunk.ma < prev    next >
Text File  |  1988-04-18  |  13KB  |  653 lines

  1. #
  2. # compufunk
  3. #
  4. # setup:
  5. # 3 voicelists
  6. # vco 1: bass voice
  7. #    Determines overall harmonic structure. Current chord note
  8. #    is stored in a global. Melody uses this to improvise off of.
  9. #    There are two base forms. Both consist of various bass riffs
  10. #    stored as procedural data (coded up). The first form has
  11. #    very little improv. The second pieces its riffs together
  12. #    in a random fashion.
  13. # vco 2: keyboard reader, maps a key to various changes
  14. #        should be plugged into melody player channel at MIDI level
  15. #     since various keys may change the melody patches.
  16. # vco 3: melody player, there are a number of melody forms. See
  17. #    the key description. The melody follows the bass voice.
  18. # vco 4: optional double of melody code.
  19. #
  20. # various keys cause various transformations.
  21. #
  22. # Idea is to play bass and jam against it with solo instrument.
  23. # melody can be switched in and out, can play duet with it.
  24.  
  25. #
  26. # harmonic structure:
  27. #
  28. # Two parts
  29. #    1. 8 bar melody lead. Melody plays horns in fifths and
  30. #    uses fairly staid melody form.
  31. #    2. 12 bar blues form. 
  32. #    Harmonic structure is
  33. #    E E E E A A E E C D E E
  34. #    all minor 7th chords. Suggest using polytonality by
  35. #    playing chord notes of minor 7th that is a fifth higher.
  36. #
  37.  
  38. # key transformations
  39. #
  40. # a key: - force one round of "theme" 8 bar melody lead.
  41. # c key: - melody form 1. dorian/blues pentatonic random melody
  42. # d key: - turn melody generation OFF
  43. # e key: - melody form 2. pentatonic chords
  44. # f key: - melody form 3. "wave" melody, ascending arpeggios
  45. # g key: - melody form 4. fractal melody
  46. # h key: - melody form 5. dorian/polytonal melody (add a 7th sometimes)
  47. # p key: - enable random switches between patches for melody voice.
  48. # o key: - enable random switches between melody forms.
  49. # m key: - make random change on patch for melody - use melody patch set.
  50. # r key: - make random change on patch for melody - use rhythm patch set.
  51. # s key: - turn second melody on (toggle)
  52.  
  53.  
  54. #
  55. # constants
  56. #
  57. NOMODES = 4
  58. OCTAVE = 12
  59. FIFTH  = 7
  60. PENTA  = 5
  61. JAZZMINOR = 7
  62. MAXRHYTHM = 5              # random rhythmic patches to choose from
  63. MAXMEL    = 15             # random melody patches to choose from
  64.  
  65. ACHAR = 0x61
  66. BCHAR = 0x62
  67. CCHAR = 0x63
  68. DCHAR = 0x64
  69. ECHAR = 0x65
  70. FCHAR = 0x66
  71. GCHAR = 0x67
  72. HCHAR = 0x68
  73. ICHAR = 0x69
  74. JCHAR = 0x6a
  75. KCHAR = 0x6b 
  76. LCHAR = 0x6c
  77. MCHAR = 0x6d
  78. NCHAR = 0x6e
  79. OCHAR = 0x6f
  80. PCHAR = 0x70
  81. QCHAR = 0x71        # don't use      
  82. RCHAR = 0x72
  83. SCHAR = 0x73
  84.  
  85.  
  86. # global vars
  87.  
  88. uchar mode             # melody mode
  89. uchar chordbase        # fundamental of current chord
  90. uchar pentascale[5]
  91. uchar minorscale[7]
  92. uchar solocount
  93. uchar basemode         # bass harmonic/tempo mode
  94. uchar pkey             # set if patch change desired
  95. uchar okey             # set if mode changes should be automatic
  96. uchar lastnote
  97. uchar solo2on
  98. uchar solopatch
  99.  
  100. uchar chordProg[12]
  101.  
  102. # fb01 patch groups
  103. #
  104. uchar fbrhythm[MAXRHYTHM]
  105. uchar fbmelody[MAXMEL]
  106.  
  107. # functions
  108.  
  109. # setup fb01 patches to randomly choose from
  110. # all are bank 1
  111. #
  112. riff initpatch()
  113.     fbrhythm[0] = 31       # steeldrum
  114.     fbrhythm[1] = 32       # timpani
  115.     fbrhythm[2] = 22       # xylo
  116.     fbrhythm[3] = 43       # snare
  117.     fbrhythm[4] = 45       # tomtom
  118.  
  119.     fbmelody[0] = 33
  120.     fbmelody[1] = 34
  121.     fbmelody[2] = 37
  122.     fbmelody[3] = 38
  123.     fbmelody[4] = 40
  124.     fbmelody[5] = 41
  125.     fbmelody[6] = 42
  126.     fbmelody[7] = 2
  127.     
  128.     fbmelody[8] = 8
  129.     fbmelody[9] = 9
  130.     fbmelody[10] = 16
  131.     
  132.     fbmelody[11] = 18
  133.     fbmelody[12] = 20
  134.     fbmelody[13] = 7
  135.     fbmelody[14] = 11
  136.  
  137. end
  138.  
  139. riff initHarmony()
  140.     chordProg[0] = LE
  141.     chordProg[1] = LE
  142.     chordProg[2] = LE
  143.     chordProg[3] = LE
  144.     chordProg[4] = LA
  145.     chordProg[5] = LA
  146.     chordProg[6] = LE
  147.     chordProg[7] = LE
  148.     chordProg[8] = LC
  149.     chordProg[9] = LD
  150.     chordProg[10] = LE
  151.     chordProg[11] = LE
  152. end
  153.  
  154. riff init()
  155.     pentascale[0] = 3
  156.     pentascale[1] = 5
  157.     pentascale[2] = 7
  158.     pentascale[3] = 10
  159.     pentascale[4] = 12
  160.  
  161.     minorscale[0] = 2
  162.     minorscale[1] = 3
  163.     minorscale[2] = 5
  164.     minorscale[3] = 7
  165.     minorscale[4] = 9
  166.     minorscale[5] = 10
  167.     minorscale[6] = 12
  168. end
  169.  
  170. riff choosetwo(t1,t2)
  171.     if ( mrand() < 127 )
  172.         return(t1)
  173.     else
  174.         return(t2)
  175.     end
  176. end
  177.  
  178. ########################################
  179. # base melody fragments
  180. #
  181.  
  182. # helper function for below b1, simple variation
  183. riff subb1()
  184.     LE  tq  105
  185.     LE  te  90
  186.     E   tq  choosetwo(105,100)
  187.     LE  te  90
  188.     D   tq  choosetwo(105,100)
  189.     LE  te  90
  190.     E   tq  choosetwo(105,100)
  191.     LB  te  94
  192. # m2
  193.     LE    tq    choosetwo(110,105)
  194.     LE    te
  195.     E    q       choosetwo(105,95)
  196.     LD    tq      choosetwo(100,90)
  197.      LB    te      90
  198.         if ( mrand() < 120 )
  199.         LD    tq      100
  200.         LB    te      90
  201.     else
  202.         LA    tq      100
  203.         LG    te      90
  204.     end
  205. end
  206.  
  207. # 2 measures
  208. riff b1(base)
  209.     chordbase = base
  210.     if ( base == LE && mrand() < 50 )
  211.         void subb1()
  212.         return(1)
  213.     end
  214.     if ( mrand() < 50 )
  215.         base    q   mrandrange(115,121)
  216.     else
  217.         base tq mrandrange(115,117)
  218.         base te  90
  219.     end
  220.     base+12 q   100
  221.     base+10 h   mrandrange(90,100)
  222. # m2
  223.     base    q   mrandrange(110,117)
  224.     base+12 q   100
  225. #
  226.     base+10 tq  mrandrange(90,110)
  227.     base+7  te  100
  228. # m2, b4
  229.     base+5  tq  100
  230.     if ( mrand() < 100 )
  231.         base+3  te  100
  232.     else
  233.         base+3  te  90
  234.     end
  235. end
  236.  
  237.  
  238. # similar to b1, but only a measure long
  239. riff b5(base)
  240.     chordbase = base
  241.     base    q   120
  242.     base+12 q   100
  243.     if ( mrand() < 100 )
  244.         base+10 h   100
  245.     else
  246.         base+10 tq  mrandrange(115,100)  
  247.         base+10 te  90
  248.         base+7  q
  249.     end
  250. end
  251.  
  252. # second measure of b1
  253. riff b6(base)
  254.     chordbase = base        
  255.     base    q   115
  256.     base+12 q   100
  257.     base+10 tq  105
  258.     base+7  te  100
  259.     base+5  tq  100
  260.     base+3  te  100
  261. end
  262.  
  263. # 1 measure
  264. riff b3(base)
  265.     chordbase = base
  266.     base    tq  mrandrange(90,115)
  267.     base    te  mrandrange(80,95)
  268.     base+12 tq  mrandrange(90,105)
  269.     base+12 te  85
  270.     base+10 te  105 
  271.     base+12 te  90
  272.     base+10 te
  273.     base+5  te  105
  274.     base+7  te
  275.     base+10 te
  276. end 
  277.  
  278. #1 measures
  279. riff b4(base)
  280.     chordbase = base
  281.     base+12  tq  110
  282.     base+12  te  100
  283.     base+3   tq  105
  284.     base+3   te  100
  285.     base+5   tq  110
  286.     base+5   te  100
  287.     base+7   tq  110
  288.     base+7   te  100
  289. end
  290.  
  291. riff b7(base)
  292.     chordbase = base
  293.     base    tq    110 
  294.     base    te       90
  295.     base+3  tq      105
  296.     base+3  te       90
  297.     base+5  tq      100
  298.     base+5  te      107
  299.     base+7  tq      mrandrange(110,115)
  300.     base+7  te      100
  301. end
  302.  
  303. riff specialB()
  304.     chordbase = LC
  305.     LC    q   110
  306.     LC+12   q   100
  307.     LC+10   h   95
  308.     chordbase = LD
  309.     LD    q   110
  310.     LD+12    q   90
  311.     LD+10 tq  105
  312.     LD+7  te  100     
  313.     LD+5  tq  100     
  314.     LD+3  te  100 
  315. end
  316.  
  317. # end of bass melody fragments
  318. #
  319.  
  320. riff solotime()
  321.     if ( {mrand()%2} == 0 )
  322.         return(tq) 
  323.     else
  324.         return(te)
  325.     end
  326. end
  327.  
  328.  
  329. riff choosethree(t1,t2,t3)
  330.     uchar rno
  331.     rno = mrand()
  332.     if ( rno < 85 )
  333.         return(t1)
  334.     end
  335.     if ( rno < 170 )
  336.         return(t2)
  337.     end
  338.     return(t3)
  339. end
  340.  
  341. #
  342. # choose a note out of one of the two scales
  343. # add an octave to base to get note up into middle range
  344. #
  345. riff solonote()
  346.     uchar index
  347.     
  348.     if ( {solocount % 4} < 2) 
  349.         return(pentascale[mrand()%PENTA]+ OCTAVE + chordbase)
  350.     else
  351.         return(minorscale[mrand()%JAZZMINOR] + OCTAVE + chordbase)
  352.     end
  353. end
  354.  
  355. #
  356. # play so many measures of solo melody
  357. #
  358. riff dosolo()
  359.     uchar mcount
  360.     uchar time
  361.     uchar solocount
  362.     uchar tcount
  363.     uchar nnote
  364.     uchar vel
  365.  
  366.     solocount++
  367.     if ( mode == 5 )
  368.         solopatch = 0
  369.         patch 0
  370.     end
  371.     for ( mcount = 0; mcount < 6; mcount++)
  372.         # check to see if random mode changes are
  373.         # desired
  374.         if ( mcount == 3 && okey )
  375.             mode = {mrand() % NOMODES} + 1
  376.         end
  377.         for ( tcount = 0; tcount < w; )
  378.             time = solotime()
  379.             if ( mode == 1 )
  380.                 solonote() time  mrandrange(90,115)
  381.             else 
  382.             if ( mode == 2 )
  383.                 vel = mrandrange(90,120)
  384.                 nnote = solonote() 
  385.                 if ( mrand() < 150 )
  386.                     chord nnote,nnote+12,nnote+24 end time + 12
  387.                 else
  388.                     chord nnote,nnote+7,nnote+12,nnote+14,nnote+24 end time + 12
  389.                 end
  390.                 time = time + 12
  391.             else # mode is 3
  392.             if ( mode == 3 )
  393.                 nnote =     solonote()
  394.                 vel = mrandrange(80,110)
  395.                 nnote         s   vel                  
  396.                 nnote + 2    t   vel + 2
  397.                  nnote + 5        s   vel + 4
  398.                 nnote + 7        t   vel + 6
  399.                 nnote + 12       s   vel + 8
  400.                 nnote + 17       t   vel + 10
  401.                 nnote + 19       s   vel + 12
  402.                 nnote + 24       t   vel + 14
  403.             else # mode is 4, fractal melody 
  404.             if ( mode == 4 )
  405.                 lastnote = mfractal1(lastnote)
  406.                 vel++
  407.                 if ( vel > 115 )
  408.                     vel = 85
  409.                 end
  410.                 chordbase+pentascale[lastnote%PENTA]+24 choosethree(s,e,h) vel
  411.             else # mode is 5, basemode special melody
  412.             if ( mode == 5 )
  413.                 if ( solopatch != 0 )
  414.                     patch 0
  415.                     solopatch = 0
  416.                 end
  417.                 nnote = solonote()
  418.                 time = solotime()
  419.                 vel = mrandrange(100,120)
  420.                 chord nnote,nnote+7 end time vel
  421.             # mode 6, venture into polytonal up a 5th
  422.             else
  423.                 if ( mrand() < 190 )
  424.                     nnote = solonote() + 7
  425.                 else
  426.                     nnote = solonote() 
  427.                 end
  428.                 nnote solotime() mrandrange(90,120)
  429.             end
  430.             end
  431.             end
  432.             end
  433.             end
  434.             tcount = tcount + time
  435.         end
  436.     end
  437.     # if pkey set however random changes 1..33
  438.     if ( pkey ) 
  439.         solopatch = {mrand() % 32} + 1
  440.         patch solopatch
  441.     end
  442. end
  443.  
  444. # first baseform
  445. # not much variation in riffs
  446. #
  447. riff baseform1()     
  448.     void b1(LE)
  449.     void b1(LE)
  450.     void b1(LA)
  451.     void b1(LE)
  452.     if ( mrand() < 200 )
  453.         void specialB() 
  454.     else
  455.         void b1(LB)
  456.     end
  457.     void b1(LE)
  458. end
  459.  
  460. # in charge of varying the bass melody
  461. #
  462. riff baseform2()
  463.     uchar rval
  464.     uchar chordindex
  465.     uchar lastval
  466.  
  467.     lastval = 1
  468.     for ( chordindex = 0; chordindex < 12; chordindex++)
  469.         rval = mrand() % 5
  470.         # try again if riff number matches, should reduce
  471.         # frequency of repeats
  472.         if (lastval == rval)
  473.             rval = mrand() % 5
  474.         end
  475.  
  476.         if ( rval == 1 )
  477.             void b3(chordProg[chordindex])
  478.         else
  479.         if ( rval == 2 )
  480.             void b4(chordProg[chordindex])
  481.         else
  482.         if ( rval == 3 )
  483.             void b5(chordProg[chordindex])
  484.          else
  485.         if ( rval == 4 )
  486.             void b6(chordProg[chordindex])
  487.         else
  488.              void b7(chordProg[chordindex])
  489.         end
  490.         end
  491.         end
  492.         end
  493.         lastval = rval
  494.     end # for
  495. end
  496.  
  497. ###################################################### 
  498. # VOICELISTS
  499. ###################
  500. #
  501. # bass is first voice.
  502. #
  503. # determines harmonic structure.
  504. # The base tone of the current chord is saved by any routines
  505. # called by the "bass" voicelist. Melody generation uses this
  506. # to generate melodic fragments.
  507. #
  508. vco bass
  509.     uchar index
  510.     uchar firstcall
  511.  
  512.     okey = 0
  513.     pkey = 0
  514.     mode = 0        # solo starts off
  515.     basemode = 1
  516.     solocount = 0
  517.     lastnote = LE
  518.     void init()
  519.     void initHarmony()
  520.     void initpatch()
  521.     firstcall = 1
  522.  
  523.     # now start to play bass variations unless n key is
  524.     # typed. 
  525.     # 'n' means normal form again
  526.     for (;;)
  527.         # if basemode is set then play 8 bar "line"
  528.         
  529.         if ( basemode )
  530.             mode = 5
  531.             void b1(LE)
  532.             void b1(LG)
  533.             void b1(LA)
  534.             void b1(LE)
  535.             void b1(LE)
  536.             void b1(LC)
  537.             void b1(LD)
  538.             void b1(LE)
  539.             mode = 4
  540.             basemode = 0
  541.         # else normal improv section
  542.         else
  543.             if ( firstcall )
  544.                 firstcall = 0
  545.                 void baseform1()
  546.             else
  547.             # small percent of normal base playing
  548.                 if ( mrand() < 10 )
  549.                     void baseform1()
  550.                 else
  551.             # great deal of improv base playing
  552.                     void baseform2()
  553.                 end
  554.             end
  555.         end
  556.     end
  557. end
  558.  
  559. #
  560. # check the keyboard
  561. # if a char is received turn on the solo channel
  562. # or turnoff the solo  if it is playing
  563. #
  564. vco readKey
  565.  
  566.     uchar input
  567.     uchar patchVal
  568.  
  569.     #
  570.     # check every quarter note
  571.     for(;;)
  572.         if (misch()) 
  573.             input = mgetch()
  574.             if ( input == ACHAR )
  575. void printf("%d\n",1)
  576.                 basemode = 1
  577.                 okey = 0
  578.                 pkey = 0
  579.             end
  580.             if ( input == CCHAR )
  581.                 mode = 1
  582.             end
  583.             if ( input == DCHAR )
  584.                 mode = 0
  585.             end
  586.             if ( input == ECHAR )
  587.                 mode = 2
  588.             end
  589.             if ( input == FCHAR )
  590.                 mode = 3
  591.             end
  592.             if ( input == GCHAR )
  593.                 mode = 4
  594.             end
  595.             if ( input == HCHAR )
  596.                 mode = 6
  597.             end
  598.             if ( input == PCHAR )
  599.                 pkey = ~pkey    # toggle
  600. void printf("%d\n",pkey)
  601.             end
  602.             if ( input == OCHAR )
  603.                 okey = ~okey    # toggle
  604. void printf("%d\n",okey)
  605.             end
  606.             # random change of melody patch
  607.             if ( input == MCHAR )
  608.                 solopatch = fbmelody[mrand()%MAXMEL]-1
  609.                 patch solopatch
  610.             end
  611.             # random change to rhythm patch
  612.             if ( input == RCHAR )
  613.                 solopatch = fbrhythm[mrand()%MAXRHYTHM]-1
  614.                 patch solopatch
  615. void printf("%d\n",patchVal)
  616.             end
  617.             if ( input == SCHAR )
  618.                 solo2on = ~solo2on
  619.             end
  620.         else
  621.             REST    q
  622.         end
  623.     end
  624. end
  625.  
  626. #
  627. # play a solo when mode is non-zero
  628. #
  629. vco solo
  630.     for(;;)
  631.         if ( mode )
  632.             void dosolo()
  633.         else
  634.             REST     w
  635.         end
  636.     end
  637. end
  638.  
  639. # optional double of solo voice
  640. #
  641. vco solo2
  642.     solo2on = 0
  643.     
  644.     for(;;)
  645.         if ( solo2on )
  646.             void dosolo()
  647.         else
  648.             REST     w
  649.         end
  650.     end
  651. end
  652.  
  653.